﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Collections.Generic;
using IronSuit.Crazy250API.LoginAPI;
using IronSuit.Utility;
using IronSuit.Crazy250API.BaseLogic;

namespace IronSuit.Crazy250Web
{
	/// <summary>
	/// Summary description for Crazy250API
	/// </summary>
	[WebService(Namespace = "http://tempuri.org/")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[ToolboxItem(false)]
	// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
	[System.Web.Script.Services.ScriptService]
	public class Crazy250API : System.Web.Services.WebService
	{
		[WebMethod(false)]
		public APIResult QueryAPI(List<APIItem> InputList)
		{
			APIResult r = new APIResult();
			Dictionary<string, string> inputDic = Convert2Dic(InputList);
			try
			{
				switch (inputDic["Method"])
				{
					case "CheckLogin":
						r.DataList = LoginHelper.CheckLogin(inputDic);						
						break;
					case "Register":
						LoginHelper.Register(inputDic);
						break;
					case "Login":
						r.DataList = LoginHelper.Login(inputDic);						
						break;
					case "QueryRes":
						break;
					default:
						break;
				}
			}
			catch (CrazyException ce)
			{
				r.ResultCode = ce.ErrorCode ;
				r.ResultMessage = ce.Message;
				return r;
			}
			catch (Exception e)
			{
				r.ResultCode = "9999";
				r.ResultMessage = "发生未知名的错误，请联系管理员！";
				LogHelper.Error("QueryAPI", "发生未知名的错误", e);
				return r;
			}
			r.ResultCode = "0";
			r.ResultMessage = "Success";			
			return r;
		}

		private Dictionary<string, string> Convert2Dic(List<APIItem> InputList)
		{
			Dictionary<string, string> dic = new Dictionary<string, string>();
			foreach (APIItem input in InputList)
			{
				if (input != null)
					dic[input.Key] = input.Value;
			}
			return dic;
		}
	}

	
	public class APIResult
	{
		public string ResultCode;
		public string ResultMessage;
		public List<APIItem> DataList;
	}
}
